package com.xywy.bloodandroid.act;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.GetRequest;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;
import com.xywy.bloodandroid.vollery.VolleyManager;

/*******************************************
 * @Title: BPNewsActivity.java
 * @Package com.xywy.bloodandroid.act
 * @Description: TODO血压咨讯
 * @author Benwolf
 * @date 2014-12-26 下午1:35:30
 * @version V1.0
 ********************************************/
public class BPNewsActivity extends BaseActivity {

	private RelativeLayout back;
	private TextView title;
	private TextView tv_hbp_commond_sense;
	private TextView tv_hbp_cookbook;
	private TextView tv_hbp_prevent;
	private TextView tv_hbp_treat;
	private TextView tv_hbp_check;
	private String typeid;
	private String dirStr;

	@Override
	protected void initView(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_bp_news);
		back = (RelativeLayout) findViewById(R.id.left_img);
		title = (TextView) findViewById(R.id.middle_title);
		title.setText(getResources().getString(R.string.bp_news_str));
		tv_hbp_commond_sense = (TextView) findViewById(R.id.tv_hbp_commond_sense);
		tv_hbp_cookbook = (TextView) findViewById(R.id.tv_hbp_cookbook);
		tv_hbp_prevent = (TextView) findViewById(R.id.tv_hbp_prevent);
		tv_hbp_treat = (TextView) findViewById(R.id.tv_hbp_treat);
		tv_hbp_check = (TextView) findViewById(R.id.tv_hbp_check);

	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub
		back.setOnClickListener(this);
		tv_hbp_commond_sense.setOnClickListener(this);
		tv_hbp_cookbook.setOnClickListener(this);
		tv_hbp_prevent.setOnClickListener(this);
		tv_hbp_treat.setOnClickListener(this);
		tv_hbp_check.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.left_img:
			finish();
			break;

		case R.id.tv_hbp_commond_sense:
			typeid = "18031";
			dirStr = "zhuanti_nk";
			getArticleList();
			break;
		case R.id.tv_hbp_cookbook:
			typeid = "7938";
			dirStr = "zhuzhan_ys";
			getArticleList();
			break;
		case R.id.tv_hbp_prevent:
			typeid = "18033";
			dirStr = "zhuanti_nk";
			getArticleList();
			break;
		case R.id.tv_hbp_treat:
			typeid = "18035";
			dirStr = "zhuanti_nk";
			getArticleList();
			break;
		case R.id.tv_hbp_check:
			typeid = "18032";
			dirStr = "zhuanti_nk";
			getArticleList();
			break;

		default:
			break;
		}
	}

	// 获取资讯列表
	private void getArticleList() {
		// TODO Auto-generated method stub

		String URL = Constants.getArticleListUrl(Constants.BP_NEWS_URL);
		StringBuilder url = new StringBuilder();
		url.append(URL);
		url.append("&typeid=" + typeid);
		url.append("&dir=" + dirStr);
		System.out.println(url.toString());
		GetRequest<String> etArticleListGet = new GetRequest<String>(
				url.toString(), String.class,
				new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						UIUtil.DissDialog(context, waittingDialog);

					}

					@Override
					public void onSuccess(String response) {
						// TODO Auto-generated method stub
						UIUtil.DissDialog(context, waittingDialog);
						JSONObject json;
						try {
							json = new JSONObject(response);
							if (json.optInt("code") == 10000) {
								Intent intent = new Intent(context,
										ArticeListActivity.class);
								intent.putExtra("data", json.optJSONArray("data").toString());
								intent.putExtra("dirStr", dirStr);
								intent.putExtra("typeid", typeid);
								startActivity(intent);
							}
							System.out.println("****************************"
									+ json.toString());
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
		etArticleListGet.setIsParseJson(false);
		VolleyManager.addRequest(etArticleListGet, this);
		executeRequest(etArticleListGet);

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

}
